<?php
/*************************************************

短信发送公共控制器 - the 
Author: WangHuabin <17-10-20>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class SmsController extends Controller {

    public function send_sms_reg(){
            $code = I('post.code_img');
            $result =R('User/check_verify',array($code));
            if($result){
                $phone = I('post.phone');
                
                 if($phone){
                    
                    //查询用户是否可以发送短信
                    $map['phone'] = $phone;
                    $map['status'] =0;
                     $map['role'] =1;
                   
                    $data = D("Verification")->get_one($map);
                    $startday = strtotime(date('Y-m-d 0:0:0',time()));
                    $where['add_time'] = array('gt',$startday);
                    $where['phone'] = $phone;
                    $msgcount = C('MSG_COUNT_USER');
                    $ip = get_client_ip_user();

                     $count = D("Verification")->count($where);
                    if($count>=$msgcount){
                        $data = array('err'=>2,'msg'=>"验证码发送次数过多,请明天再试！");
                        $this->ajaxReturn($data);
                    }else{
                        $where = array();
                        $where['add_time'] = array('gt',$startday);
                        $where['phone'] = $phone;
                        $where['ip'] = $ip;
                        $count = D("Verification")->count($where);
                        if($count>=$msgcount){
                            $data = array('err'=>3,'msg'=>"当前ip发送次数过多！");
                            $this->ajaxReturn($data);
                        }else{
                            if($data['add_time']<=(time()-60)){
                                //发送验证码 
                               $code = rand(111111,999999);
                               $res = R('Send/sendsms',array($phone,$code,$type = 1));
                               //$res = array('err'=>0);
                                if($res['err']==0){
                                    //记录短信发送
                                    $msgdata['type']=1;
                                    $msgdata['role']=1;
                                    $msgdata['phone']=$phone;
                                    $msgdata['status']=0;
                                    $msgdata['add_time']=time();
                                    $msgdata['time_failure']=time()+300;
                                    $msgdata['code']=$code;
                                    $msgdata['ip']=$ip;
                                    $res = D("Verification")->add($msgdata);

                                    if($res){
                                        $data = array('err'=>0,'msg'=>"发送成功！");
                                        $this->ajaxReturn($data);
                                    }else{
                                        $data = array('err'=>5,'msg'=>"发送失败！");
                                        $this->ajaxReturn($data);
                                    }

                                }else{

                                    $data = array('err'=>6,'msg'=>"发送失败，接口异常！");
                                    $this->ajaxReturn($data);

                                }
                            }else{
                                $data = array('err'=>4,'msg'=>"发送频率过快，休息一会！");
                                $this->ajaxReturn($data);
                            }
                        }
                    }
                 }

            }else{
                $data = array('err'=>1,'msg'=>"图形验证码错误！");
                $this->ajaxReturn($data);
            }
    }

    //找回密碼發送短信
     public function pass_send_sms_reg(){

        $code = I('post.code_img');
        $result =R('User/check_verify',array($code));

        if($result){
            $phone = I('post.phone');
            $role = I('post.role');
             if($phone){
                //查询用户是否可以发送短信
                //查询手机号码是否已注册
                $map['user_phone'] = $phone;
                $info = D('User')->get_one($map);
                if(!$info){
                    $data = array('err'=>7,'msg'=>"该号码未注册！");
                    $this->ajaxReturn($data);
                }

                $map['phone'] = $phone;
                $map['status'] =0;
                $map['role'] =$role;
                $data = D("Verification")->get_one($map);
                $startday = strtotime(date('Y-m-d 0:0:0',time()));
                $where['add_time'] = array('gt',$startday);
                $where['phone'] = $phone;
                $msgcount = C('MSG_COUNT_USER');
                $ip = get_client_ip_user();

                $count = D("Verification")->count($where);
                if($count>=$msgcount){
                    $data = array('err'=>2,'msg'=>"验证码发送次数过多,请明天再试！");
                    $this->ajaxReturn($data);
                }else{
                    $where = array();
                    $where['add_time'] = array('gt',$startday);
                    $where['phone'] = $phone;
                    $where['ip'] = $ip;
                    $count = D("Verification")->count($where);
                    if($count>=$msgcount){
                        $data = array('err'=>3,'msg'=>"当前ip发送次数过多！");
                        $this->ajaxReturn($data);
                    }else{
                        if($data['add_time']<=(time()-300)){
                            //发送验证码
                            $code = rand(111111,999999);
                           $res = R('Send/sendsms',array($phone,$code,$type = 1));
						   //print_r($res);
                           // $res = array('err'=>0);
                            if($res['err']==0){
                                //记录短信发送
                                $msgdata['type']=1;
                                $msgdata['role']=3;
                                $msgdata['phone']=$phone;
                                $msgdata['status']=0;
                                $msgdata['add_time']=time();
                                $msgdata['time_failure']=time()+300;
                                $msgdata['code']=$code;
                                $msgdata['ip']=$ip;
                                $res = D("Verification")->add($msgdata);

                                if($res){
                                    $data = array('err'=>0,'msg'=>"发送成功！");
                                    $this->ajaxReturn($data);
                                }else{
                                    $data = array('err'=>5,'msg'=>"发送失败！");
                                    $this->ajaxReturn($data);
                                }

                            }else{

                                $data = array('err'=>6,'msg'=>"发送失败，接口异常！");
                                $this->ajaxReturn($data);

                            }
                        }else{
                            $data = array('err'=>4,'msg'=>"发送频率过快，休息一会！");
                            $this->ajaxReturn($data);
                        }
                    }
                }
             }

        }else{
            $data = array('err'=>1,'msg'=>"图形验证码错误！");
            $this->ajaxReturn($data);
        }
    }

        //修改手机号
     public function change_phone_sms(){
            $phone = I('post.phone');          
            $role = I('post.role');
            $val = I('post.val');
             if($phone){
                
                //查询手机号码是否已注册
                $map['user_phone'] = $phone;
                $info = D('User')->get_one($map);
                if($val){
                    if(!$info){
                        $data = array('err'=>7,'msg'=>"该号码未注册！");
                        $this->ajaxReturn($data);
                    }
                }else{
                     if($info){
                        $data = array('err'=>7,'msg'=>"该号码已注册！");
                        $this->ajaxReturn($data);
                    }
                }
                $map['phone'] = $phone;
                $map['status'] =0;
                $map['role'] =$role;
                $data = D("Verification")->get_one($map);
                $startday = strtotime(date('Y-m-d 0:0:0',time()));
                $where['add_time'] = array('gt',$startday);
                $where['phone'] = $phone;
                $msgcount = C('MSG_COUNT_USER');
                $ip = get_client_ip_user();

                 $count = D("Verification")->count($where);
                 
                if($count>=$msgcount){
                    $data = array('err'=>2,'msg'=>"验证码发送次数过多,请明天再试！");
                    $this->ajaxReturn($data);
                }else{
                    $where = array();
                    $where['add_time'] = array('gt',$startday);
                    $where['phone'] = $phone;
                    $where['ip'] = $ip;
                    $count = D("Verification")->count($where);
                    if($count>=$msgcount){
                        $data = array('err'=>3,'msg'=>"当前ip发送次数过多！");
                        $this->ajaxReturn($data);
                    }else{
                        if($data['add_time']<=(time()-60)){
                            //发送验证码
                            $code = rand(111111,999999);
                            $res = R('Send/sendsms',array($phone,$code,$type = 1));
                        //   $res = array('err'=>0);
                            if($res['err']==0){
                                //记录短信发送
                                $msgdata['type']=1;
                                $msgdata['role']=3;
                                $msgdata['phone']=$phone;
                                $msgdata['status']=0;
                                $msgdata['add_time']=time();
                                $msgdata['time_failure']=time()+300;
                                $msgdata['code']=$code;
                                $msgdata['ip']=$ip;
                                $res = D("Verification")->add($msgdata);

                                if($res){
                                    $data = array('err'=>0,'msg'=>"发送成功！");
                                    $this->ajaxReturn($data);
                                }else{
                                    $data = array('err'=>5,'msg'=>"发送失败！");
                                    $this->ajaxReturn($data);
                                }

                            }else{

                                $data = array('err'=>6,'msg'=>"发送失败，接口异常！");
                                $this->ajaxReturn($data);

                            }
                        }else{
                            $data = array('err'=>4,'msg'=>"发送频率过快，休息一会！");
                            $this->ajaxReturn($data);
                        }
                    }
                }
             }

        
    }



}